using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempSpacesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempSpacesDbo : DatabaseObject
        {
            public TempSpacesDbo()
            {
                Name = "TempSpaces";
                GeneratedClassName = "TempSpaces";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "StoreId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "StoreName", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "StoreName" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "ArticleId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PlanogramId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "PlanogramId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Planogram", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Planogram" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleCode", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleCode" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UOM", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "UOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ArticleName", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "ArticleName" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "MC", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "MC" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Category", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Category" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Facings", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Facings" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Width", ColumnType = "smallmoney", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Width" });
              
            }
        }
		
		public static List<TempSpaceRow> Select(Expression<Func<TempSpaceRow, bool>> expression)
        {
            List<TempSpaceRow> rows = new List<TempSpaceRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempSpaceRow>(new TempSpacesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempSpaceRow> Select()
		{
			List<TempSpaceRow> rows = new List<TempSpaceRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempSpaceRow>(new TempSpacesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempSpaceRow> PagedSelect(Expression<Func<TempSpaceRow, bool>> filterExpression, Expression<Func<TempSpaceRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempSpaceRow> rows = new List<TempSpaceRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempSpaceRow>(new TempSpacesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempSpaceRow> TempSpaceList)
        {
            TempSpaceRow row;

            while (reader.Read())
            {
                row = new TempSpaceRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					StoreId = Util.CastDbValue<int?>(reader["StoreId"]),
					StoreCode = Util.CastDbValue<string>(reader["StoreCode"]),
					StoreName = Util.CastDbValue<string>(reader["StoreName"]),
					ArticleId = Util.CastDbValue<int?>(reader["ArticleId"]),
					PlanogramId = Util.CastDbValue<int?>(reader["PlanogramId"]),
					Planogram = Util.CastDbValue<string>(reader["Planogram"]),
					ArticleCode = Util.CastDbValue<string>(reader["ArticleCode"]),
					UOM = Util.CastDbValue<string>(reader["UOM"]),
					ArticleName = Util.CastDbValue<string>(reader["ArticleName"]),
					MC = Util.CastDbValue<string>(reader["MC"]),
					Category = Util.CastDbValue<string>(reader["Category"]),
					Facings = Util.CastDbValue<int?>(reader["Facings"]),
					Width = Util.CastDbValue<decimal>(reader["Width"])
                };
                                
                TempSpaceList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempSpaceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempSpaceRow>(new TempSpacesDbo(), row);
            }
        }

		public static void Update(TempSpaceRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempSpaceRow>(new TempSpacesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempSpaceRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempSpaceRow>(new TempSpacesDbo(), expression);
            }
        }

	}
}
	
